package com.hanbang.mrcm.home;

import java.util.ArrayList;
import java.util.Collection;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.hanbang.mrcm.R;
import com.hanbang.mrcm.bean.HomeItemData;
import com.hanbang.mrcm.bean.HomeTypeData;
import com.hanbang.mrcm.myview.ScaleImageView;
import com.hanbang.mrcm.utils.HttpInterfaces;
import com.hanbang.mrcm.utils.JsonHelper;
import com.hanbang.mrcm.utils.MyLog;
import com.hanbang.mrcm.utils.MyRequestCallBack;
import com.hanbang.mrcm.utils.SystemTools;
import com.hanbang.mrcm.utils.UiUtils;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class HomeFragment extends Fragment {
	private PullToRefreshListView listView;
	private ArrayList<HomeItemData> listData = new ArrayList<HomeItemData>();
	private MyAdapter adapter = null;

	private HomeTypeData type = null;

	public HomeFragment(HomeTypeData data) {
		this.type = data;
	}

	@SuppressLint("InflateParams")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		listView = (PullToRefreshListView) inflater.inflate(
				R.layout.pulltto_refresh_listview, null);
		UiUtils.applyFont(listView);
		initView();
		setOnClickListener();
		getGoodList();
		return listView;
	}

	private void initView() {
		listView.getRefreshableView().setDivider(
				getActivity().getResources().getDrawable(R.color.white));
		listView.getRefreshableView().setDividerHeight(
				SystemTools.dip2px(getActivity(), 10));
		adapter = new MyAdapter();
		listView.setMode(Mode.PULL_FROM_START);
		listView.setAdapter(adapter);
		listView.setOnRefreshListener(new OnRefreshListener2<ListView>() {

			@Override
			public void onPullDownToRefresh(
					PullToRefreshBase<ListView> refreshView) {
				listData.clear();
				getGoodList();
			}

			@Override
			public void onPullUpToRefresh(
					PullToRefreshBase<ListView> refreshView) {

			}
		});
	}

	/*
	 * 设置点击事件
	 */
	private void setOnClickListener() {
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent();
				intent.putExtra("channel_id", listData.get(position - 1)
						.getChannel_id());
				intent.putExtra("category_id", listData.get(position - 1)
						.getCategory_id());
				if (listData.get(position - 1).getActivity_type() == 1) {

					intent.setClass(getActivity(), GoodListActivity.class);
					startActivity(intent);

				} else if (listData.get(position - 1).getActivity_type() == 2) {
					intent.setClass(getActivity(), InsaneClickActivity.class);
					startActivity(intent);
				}

			}
		});
	}

	private class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return listData.size();
		}

		@Override
		public Object getItem(int position) {
			return listData.get(position);
		}

		@Override
		public long getItemId(int position) {
			return listData.get(position).getChannel_id();
		}

		@SuppressLint("InflateParams")
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			HomeItemData data = (HomeItemData) getItem(position);
			ViewHolder holder = null;
			if (convertView == null) {
				convertView = getActivity().getLayoutInflater().inflate(
						R.layout.home_list_item, null);
				holder = new ViewHolder();
				holder.imageView = (ScaleImageView) convertView
						.findViewById(R.id.imageview);
				UiUtils.applyFont(convertView);
				convertView.setTag(holder);

			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			((HomeActivity) getActivity()).bitmapUtils.display(
					holder.imageView,
					SystemTools.getHttpFileUrl(data.getImg_url()));

			return convertView;
		}

		private class ViewHolder {
			ScaleImageView imageView = null;
		}
	}

	private void getGoodList() {
		MyLog.e("getGoodList",
				HttpInterfaces.getGoodList + "&type=" + type.getType());
		((HomeActivity) getActivity()).httpUtils.send(HttpMethod.GET,
				HttpInterfaces.getGoodList + "&type=" + type.getType(),
				new MyRequestCallBack<String>(getActivity(),
						getString(R.string.loadding)) {
					@Override
					public void onFailure(HttpException error, String msg) {
						super.onFailure(error, msg);
						listView.onRefreshComplete();
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						super.onSuccess(responseInfo);
						try {
							JSONObject json = new JSONObject(
									responseInfo.result);
							int status = json.getInt("status");
							if (status == 1) {
								JSONArray result = json.getJSONArray("result");
								Collection<HomeItemData> c = JsonHelper
										.parseCollection(result,
												ArrayList.class,
												HomeItemData.class);
								listData.addAll(c);
								adapter.notifyDataSetChanged();
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}
						listView.onRefreshComplete();
					}
				});
	}
}
